package org.kjtc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.kjtc.entity.Role;
import org.kjtc.service.RoleService;
import org.kjtc.util.Const;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class RoleController extends BaseController{

    @Resource(name = "roleService")
	private RoleService roleService;

    @RequestMapping(value = "searchRole")
    @ResponseBody
    public Map<String, Object> searchRole(Role role){

    	Map<String, Object> map = new HashMap<String, Object>();
    	logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "角色信息查询结束");

		List<Role> list = null;
		role.setRoleID(this.getUesrInfo().getRoleID());
		try {
			list = roleService.getRole(role);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "角色查询异常");
			return map;
		}
		map.put("roleInfoList", list);
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "角色信息查询结束");
		role = null;
		return map;
    }

    @RequestMapping(value = "saveRole")
    @ResponseBody
    public Map<String, Object> saveRole(Role role){

    	Map<String, Object> map = new HashMap<String, Object>();
    	logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "角色信息添加开始");
    	role.setCreateUser(this.getUesr().getUserID());
    	role.setUpdateUser(this.getUesr().getUserID());
    	int count = 0;
    	try {
			count = roleService.saveRole(role);
			if (count > 0) {
				map.put("result", "Success");
				this.setSessionAttribute(Const.SESSION_ROLENAME, null);
				logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "添加角色" + role.getRoleName() + "(" + role.getRoleID() + ")");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "角色信息添加异常:" + e.getMessage());
			return map;
		}
    	 logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "角色信息添加结束");
		 role = null;
		 return map;
    }
    @RequestMapping(value = "updateRole")
    @ResponseBody
    public Map<String, Object> updateRole(Role role){

    	Map<String, Object> map = new HashMap<String, Object>();
    	logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "角色信息修改开始");
    	role.setUpdateUser(this.getUesr().getUserID());
    	int count = 0;
    	try {
			count = roleService.updateRole(role);
			if (count > 0) {
				map.put("result", "Success");
				this.setSessionAttribute(Const.SESSION_ROLENAME, null);
				logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "修改角色" + role.getRoleName());
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "角色修改异常:" + e.getMessage());
			return map;
		}
    	logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "角色信息修改结束");
    	role = null;
    	return map;
    }
    @RequestMapping(value = "deleteRole")
    @ResponseBody
    public Object deleteRole(String roleID){

    	Map<String, Object> map = new HashMap<String, Object>();
    	logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "角色信息删除开始");
		String [] roleIDList = roleID.split(",");
		Role role = new Role();
		role.setRoleIDList(roleIDList);
		int count = 0;
		try {
			count = roleService.deleteRole(role);
			if (count > 0) {
				map.put("result", "Success");
				this.setSessionAttribute(Const.SESSION_ROLENAME, null);
				logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "删除角色" + role.getRoleName() + "(" + role.getRoleID() + ")");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "角色信息删除异常:" + e.getMessage());
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "角色信息删除结束");
		role = null;
		return map;
    }
}